//获取应用实例
const app = getApp();
import apiService from '../../../utils/api-service'; 
import apiResult from '../../../utils/api-result';
import { Config, ParserStyle, CustomStyle,getPhone, getInfo } from './../../../config/api';
Page({
  data: {
    parserStyle:ParserStyle,
    topImage: CustomStyle.topImage,
    logo: "",
    username: "",
    password: "",
    loginMessage: false,
    info: {},
    showAuth: true,
    userInfo: {}
  },
  async onLoad() { 
    var that = this;
    that.setData({
      logo: app.globalData.logo,
    })
  },
  async onShow() {
    var that = this;
    that.setData({
      loginMessage: false
    })
  },
  
/**
 **获取用户信息登录
 **/
  userInfoHandler(e) {
    if (e.detail.userInfo) {
      // that.setData({
      //   showAuth: false
      // })
      this.getInfo()
    } else {
      wx.showModal({
        content: "请确认授权"
      })
    }

  },
  getOpenId(appid, code, wxUser) {
    let that = this
    wx.request({
      url: getInfo(appid),
      // url: "https://web.dingn.cn/api/mini/" + appid + '/info',
      method: 'post',
      data: {
        code: code,
        wxUser: wxUser
      },
      async success(res) {
        console.log(res, 'getOpenId')
        if (res.data.data.phone) {
          wx.setStorageSync(Config.Token, res.data.data.accessToken)
          apiResult.success("登录成功");
          wx.switchTab({
            url: '/pages/admin/home/index'
          });  
         
        }
        that.setData({
          userInfo: res.data.data,
          showAuth: false
        })
      }
    })
  },
  onGetPhoneNumber(e) {
    console.log(e, 'sj')
    let appid = wx.getAccountInfoSync().miniProgram.appId;
    if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {

    } else {
      this.getPhone(appid, e.detail)
    }
  },
  getPhone(appid, val) {
    let user = this.data.userInfo
    let info = this.data.info
    let that = this
    console.log(this.data.userInfo)
    return wx.request({
      url: getPhone(appid),
      // url: "https://web.dingn.cn/api/mini/" + appid + '/phone',
      method: 'post',
      data: {
        "session_key": user.session_key,
        "encryptedData": val.encryptedData,
        "iv": val.iv,
        "appType": 0,
        "openid": user.openid,
        "unionid": user.unionid,
        "userInfo": info.userInfo
      },
      async success(res) {
        console.log(res)
        wx.setStorageSync(Config.Token, res.data.data.accessToken)
        apiResult.success("登录成功");
        wx.switchTab({
          url: '/pages/admin/home/index'
        });  
       
      }
    })
  },
  getInfo() {
    let appid = wx.getAccountInfoSync().miniProgram.appId;
    let code = ''
    let that = this
    wx.login({
      success: res => {
        code = res.code
        console.log(res, appid, 'login')
        wx.getUserInfo({
          success: (info) => {
            // this.info = info
            that.setData({
              info: info
            });
            console.log(info, 'getUserInfo')
            that.getOpenId(appid, code, info)

          }
        })
      }
    })
  },
  /**
   * end
   * 
  */

  /**
   * 输入账号
   */
  usernameInput(e){
    this.setData({
      username: e.detail.value
    });
  },
  /**
   * 输入密码
   * @param {*} e 
   */
  passwordInput(e){
    this.setData({
      password: e.detail.value
    });
  },
  /**
   * 登录
   */
  async login(){
    if(!this.data.username){
      apiResult.warn("账号不能为空");
      return ;
    }
    if(!this.data.password){
      apiResult.warn("密码不能为空");
      return ;
    }
    const param = {
      username: this.data.username,
      password: this.data.password
    }
    try {
      const result = await apiService.adminLogin(param);
      wx.setStorageSync(Config.Token, result.access_token)
      apiResult.success("登录成功");
      wx.switchTab({
        url:'/pages/admin/home/index'  
      });  
    } catch (error) {
      return error.message;
    }
  },
  /**
   * 复制
   * @param {*} e 
   */
  CopyLink(e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.link,
      success: res => {
        wx.showToast({
          title: '已复制',
          duration: 1000,
        })
      }
    })
  },
  getMessage() {
    var that = this;
    const loginMessage = that.data.loginMessage;
    if(loginMessage){
      that.setData({
        loginMessage: false
      })
    }else{
      that.setData({
        loginMessage: true
      })
    }
  }
  
})